//
//  SEFilterHeaderMenuView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/6.
//

import UIKit

enum SEFilterHeaderMenuViewStyle {
    case `default`, address
}

typealias SEFilterHeaderMenuViewClosure = (SEFilterMenuModel) -> Void

class SEFilterHeaderMenuView: UIView {
    
    var filterMenuClosure: SEFilterHeaderMenuViewClosure?
    
    private var menuStyle: SEFilterHeaderMenuViewStyle = .default
    
    private var result = SEFilterMenuModel()
    
    private var provinceModels: [AddressModel] = [] {
        didSet {
            self.leftTableView.reloadData(models: provinceModels, style: menuStyle)
        }
    }
    private var cityModels: [AddressModel] = [] {
        didSet {
            self.centerTableView.reloadData(models: cityModels, style: menuStyle)
        }
    }
    private var areaModels: [AddressModel] = [] {
        didSet {
            self.rightTableView.reloadData(models: areaModels, style: menuStyle)
        }
    }

    @IBOutlet private weak var backgroundView: UIView! {
        didSet {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(_dismissMenu))
            backgroundView.addGestureRecognizer(tapGesture)
        }
    }
    
    @IBOutlet private weak var contentViewHeightConstraint: NSLayoutConstraint!
    
    @IBOutlet private weak var leftTableView: SEFilterHeaderMenuTableView!
    @IBOutlet private weak var centerTableView: SEFilterHeaderMenuTableView!
    @IBOutlet private weak var rightTableView: SEFilterHeaderMenuTableView!
    
    func showMenu(in view: UIView, rect: CGRect, menuStyle: SEFilterHeaderMenuViewStyle = .default) {
        self.menuStyle = menuStyle
        self.frame = CGRect(x: 0, y: rect.maxY, width: rect.width, height: view.height - rect.maxY)
        view.addSubview(self)
        contentViewHeightConstraint.constant = self.frame.height * 0.5
        
        UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .curveLinear) {
            self.layoutIfNeeded()
        } completion: { _ in
            switch menuStyle {
            case .default: break
            case .address: self.loadAddressData()
            }
        }
        
        switch menuStyle {
        case .default:
            centerTableView.isHidden = true
            rightTableView.isHidden = true
            leftTableView.selectedClosure = { indexPath in
                self._dismissMenu()
            }
        case .address:
            centerTableView.isHidden = false
            rightTableView.isHidden = false
            leftTableView.selectedClosure = { indexPath in
                let provinceModel = self.provinceModels[indexPath.row]
                self.cityModels = provinceModel.children
                self.areaModels.removeAll()
                self.result.provinceId = provinceModel.id
            }
            centerTableView.selectedClosure = { indexPath in
                let cityModel = self.cityModels[indexPath.row]
                self.areaModels = cityModel.children
                self.result.cityId = cityModel.id
            }
            rightTableView.selectedClosure = { indexPath in
                let areaModel = self.areaModels[indexPath.row]
                self.result.areaId = areaModel.id
                self.result.title = areaModel.areaName
                self._dismissMenu()
            }
        }
    }
    
    @objc private func _dismissMenu() {
        dismissMenu()
        filterMenuClosure?(result)
    }
    
    func dismissMenu() {
        contentViewHeightConstraint.constant = 0.0
        
        UIView.animateKeyframes(withDuration: 0.25, delay: 0, options: .calculationModeLinear) {
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
}

extension SEFilterHeaderMenuView {
    private func loadAddressData() {
        SEHttpManager.shared.requestForModelArray(SEAddressAPI.address, model: AddressModel.self) { (models, status) in
//            self.activityView.stopAnimating()
            switch status {
            case .success:
                self.provinceModels = models ?? []
            case .auth(let msg):
                SEHUD.showError(message: msg)
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }
}
